<?php
namespace plugins\site_config\controller;
use cmf\controller\ApiPluginBaseController;
use plugins\member\model\CommModel;
use plugins\member\model\MemberModel;
use plugins\site_config\lib\Common as LCOM;
use think\Controller;
use think\Db;

class ApiIndexController extends Controller//ApiPluginBaseController
{
    //获取站点信息
    public function getSiteInfo()
    {
        //$config = LCOM::getSiteInfo();
        $config = \plugins\setting\lib\Setting::getCache( 'site_config' );
        return api_json_echo( true , 'success' , $config );
    }


    //获取该设备的记录
    public function getDeviceInfo()
    {
        $post = $this->request->post();
        if(empty($post['sn'])){
            return api_json_echo(false,'请上传设备编号sn','');
        }
        $list = Db::name('device_log')->where('sn',$post['sn'])->select()->each(function ($item){
            $item['time'] = date('Y-m-d H:i:s',$item['time']);
            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
            return $item;
        });
        return api_json_echo(true,'获取成功',$list);
    }


    public function decodeData($bloodsugar){
        $sugar = round(intval(substr($bloodsugar,26,3),10)/18,1);//血糖值
        $type = substr($bloodsugar,10,1);//type
        $mealtime = substr($bloodsugar,25,1);//mealtime
        $sys = substr($bloodsugar,20,3);//sys高压
        $dia = substr($bloodsugar,23,3);//dia低压
        $pul = substr($bloodsugar,26,3);//pul心率
        $sn = substr($bloodsugar,11,9);//SN
        $imsi = substr($bloodsugar,44,15);//IMSI
        $time = strtotime(substr(date('Y',time()), 0,2).
            substr($bloodsugar,29,2)
            .'-'.substr($bloodsugar,31,2).'-'.substr($bloodsugar,33,2).' '.substr($bloodsugar,35,2).
            ':'.substr($bloodsugar,37,2).':00');
        //加入记录
        $add['sugar'] = $sugar;
        $add['type'] = $type;
        $add['mealtime'] = $mealtime;
        $add['sys'] = $sys;
        $add['dia'] = $dia;
        $add['pul'] = $pul;
        $add['sn'] = $sn;
        $add['imsi'] = $imsi;
        $add['time'] = $time;
        $add['get_data'] = $bloodsugar;
        $add['add_time'] = time();
        Db::name('device_log')->insert($add);
        //同步到APP
        //1.根据设备号找到uid
        $memberDeviceInfo = Db::name('member_device')->where('device',$sn)->find();
        if(!empty($memberDeviceInfo)){
            $commModel = new CommModel();
            $band_data['deviceNo'] = $memberDeviceInfo['device'];
            $band_data['measureTime'] = date('Y-m-d H:i:s',$time);
            $band_data['measureResult'] = '1';
            $band_data['exceptionType'] = '0';
            if($type == '1' || $type == '01'){
                //血压
                $band_data['systolicPressure'] = $sys;
                $band_data['diastolicPressure'] = $dia;
                $band_data['heartRate'] = $pul;
                $commModel->bloodPressure($band_data);
            }
            if($type == '2' || $type == '02'){
                //血糖
                if(empty($mealtime)){
                    $mealtime = 5;
                }
                $band_data['bloodSugarType'] = $mealtime;
                $band_data['bloodSugarValue'] = $sugar;
                $commModel->bloodSugar($band_data);
            }
        }
    }

    /**
     * 获取血糖值
     */
    public function gprs_sample(){
        date_default_timezone_set("PRC");
        header("Content-type: text/html; charset=utf-8");
        header("Content-Length:31");
        if(empty($_GET) || empty($_GET['data']) ){
            echo '未获取到参数';
        }else{
            $data = htmlspecialchars(trim($_GET['data']));
            $status = $this->Ord_Verify($data);
            if($status != 200){
                echo $status;die;
            }else{

                //解析data
                $this->decodeData($data);


                $time = time();
                $ip1 = strtoupper(dechex(118));
                $ip2 = strtoupper(dechex(89));
                $ip3 = strtoupper(dechex(235));
                $ip4 = strtoupper(dechex(48));
                $post1 = strtoupper(dechex(80));
                $post2 = strtoupper(dechex(80));
                $sum1 = substr(dechex(intval($ip1,16)^intval($ip2,16)^intval($ip3,16)^intval($ip4,16)^intval($post1,16)^intval($post2,16)),0,2);
                $sum1 = strlen($sum1) < 2 ? '0'.strtoupper($sum1) : strtoupper($sum1);

                $returntime =  date('Y-m-d H:i:s',$time);
                $y = strlen(dechex(substr($returntime,2,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,2,2))) : strtoupper(dechex(substr($returntime,2,2)));
                $m = strlen(dechex(substr($returntime,5,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,5,2))) : strtoupper(dechex(substr($returntime,5,2)));
                $d = strlen(dechex(substr($returntime,8,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,8,2))) : strtoupper(dechex(substr($returntime,8,2)));
                $h = strlen(dechex(substr($returntime,11,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,11,2))) : strtoupper(dechex(substr($returntime,11,2)));
                $i = strlen(dechex(substr($returntime,14,2))) < 2 ? '0'.strtoupper(dechex(substr($returntime,14,2))) : strtoupper(dechex(substr($returntime,14,2)));
                $sum2 = substr(dechex(intval($y,16)^intval($m,16)^intval($d,16)^intval($h,16)^intval($i,16)),0,2);
                $sum2 = strlen($sum2) < 2 ? '0'.strtoupper($sum2) : strtoupper($sum2);
                $return='+IP'.$ip1.$ip2.$ip3.$ip4.$post1.$post2.$sum1.$y.$m.$d.$h.$i.$sum2.'OK';
                echo $return;
            }
        }
    }

    function Ord_Verify($str = ''){
        $str = trim($str);
        if(empty($str)){
            return '10001';
        }
        $SUMDEC = 0;
        $Verify = '';
        for ($i=0; $i < 43; $i++) {
            if($i < 39){
                $SUMDEC += ord($str[$i]);
            }else{
                $Verify .= $str[$i];
            }
        }
        $SUMDEC = strtoupper(dechex($SUMDEC));
        if(empty($SUMDEC)){
            return '10002';
        }
        if(strlen($SUMDEC) < 4){
            if(strlen($SUMDEC) == 3){
                $SUMDEC = '0'.$SUMDEC;
            }elseif(strlen($SUMDEC) == 2){
                $SUMDEC = '00'.$SUMDEC;
            }else{
                $SUMDEC = '000'.$SUMDEC;
            }
        }
        if($SUMDEC == $Verify){
            return '200';
        }else{
            return '10003';
        }
    }
    
}